$(function () {
//TODO:1.展开下一级菜单;2.菜单间距

    var deptList; //存储属性部门列表
    var deptMap = {}; //存储map格式的部门信息

    //为所有当行菜单添加点击效果-展开下一级菜单
    $(".open-next-navigation").click(function () {
        if ($(this).siblings('ul').is(':hidden')) {
            console.log("hidden");
            $(this).siblings('ul').show();
        } else {
            console.log("show");
            $(this).siblings('ul').hide();
        }
    });

    loadDeptTree();

    function loadDeptTree() {
        $.ajax({
            url: "sys/dept/tree.json",
            success: function (result) {
                if (result.status) {
                    deptList = result.data;
                    //渲染部门树
                    var ul = $("<ul></ul>");
                    $('.topMenu').append(ul);
                    $(result.data).each(function (i, dept) {
                        deptMap[dept.id] = dept;
                        var a = $('<a class="open-next-navigation" style="margin-left: 10px;"></a>');
                        a.val(dept.id);
                        a.text(dept.name);
                        // buildLi(ul, a, dept);
                   /* <a class="dept_modify" style="margin-right: 10px;float: right;">修改</a>*/
                        var li = $('<li><i class="icon-remove pull-right dept-delete"></i> <i class="icon-pencil pull-right dept-modify"></i></li>');
                        li.append(a);
                        ul.append(li);

                        recursiveRenderDept(dept.deptList, li);
                    })

                } else {
                    alert(result.message);
                }
            }
        })
    }

    //递归渲染部门树
    function recursiveRenderDept(deptList, li) {
        //TODO:
        var ul = $('<ul></ul>');
        if (deptList != null && deptList.length > 0) {
            $(deptList).each(function (i, dept) {
                deptMap[dept.id] = dept;
                var tempLi = $('<li><i class="icon-remove pull-right dept-delete"></i> <i class="icon-pencil pull-right dept-modify"></i></li>');
                var tempA = $('<a class="open-next-navigation"></a>');
                tempA.val(dept.id);
                tempA.text(dept.name);
                tempLi.append(tempA);
                ul.append(tempLi);
                li.append(ul);
                /*if (dept.deptList != null && dept.deptList.length > 0) {

                }*/
                recursiveRenderDept(dept.deptList, tempLi);
            })
        }
    }

    function buildLi(ul, a, dept) {
        var li = $('<li><a class="dept_modify" style="margin-right: 10px;float: right;">修改</a></li>');
        li.append(a);
        ul.append(li);
    }

    //修改和删除部门

    $(".dept-add").click(function () {
        alert("添加");
    })

})